package com.miao.font.web.httpclient;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;

@Service
public class IndexService {

    @Autowired
    private ApiService apiService;

    private static final ObjectMapper MAPPER=new ObjectMapper();
    /**
     * 显示页面大广告
     * @return
     * @throws IOException
     */
    public String queryIndexAD1() throws IOException {
        try{
            String url="http://manage.taotao.com/tbContent?categoryId=33&page=1&rows=6";
            String jsonData=this.apiService.doGet(url);
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            //解析json数据
            JsonNode jsonNode=MAPPER.readTree(jsonData);
            ArrayNode rows =(ArrayNode) jsonNode.get("rows");
            List<Map<String,Object>> result=new ArrayList<>();
            for (JsonNode row:rows){
                //有顺序的集合
                Map<String,Object> map=new LinkedHashMap<>();
                //row.get("pic")拿到的是一个URL地址对象
                map.put("srcB",row.get("pic").asText());
                map.put("height",240);
                map.put("alt",row.get("title").asText());
                map.put("width",670);
                map.put("src",row.get("pic").asText());
                map.put("widthB",550);
                map.put("href",row.get("url").asText());
                map.put("heightB",240);
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 显示右上角的小广告
     * @return
     * @throws IOException
     */
    public String queryIndexAD2() throws IOException {
        try{
            String url="http://manage.taotao.com/tbContent?categoryId=34&page=1&rows=1";
            String jsonData=this.apiService.doGet(url);
            if(StringUtils.isEmpty(jsonData)){
                return null;
            }
            //解析json数据
            JsonNode jsonNode=MAPPER.readTree(jsonData);
            ArrayNode rows =(ArrayNode) jsonNode.get("rows");
            List<Map<String,Object>> result=new ArrayList<>();
            for (JsonNode row:rows){
                //有顺序的集合
                Map<String,Object> map=new LinkedHashMap<>();
                map.put("width",310);
                map.put("height",70);
                map.put("src",row.get("pic").asText());
                map.put("href",row.get("url").asText());
                map.put("alt",row.get("title").asText());
                map.put("widthB",210);
                map.put("heightB",70);
                //row.get("pic")拿到的是一个URL地址对象
                map.put("srcB",row.get("pic").asText());
                result.add(map);
            }
            return MAPPER.writeValueAsString(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
}
